__author__ = "Christian UNterberg, Tetsuo Animation Studio"
__copyright__ = "Copyright 2024, Tetsuo Animation Studio"
__credits__ = ["Christian Unterberg"]
__license__ = "GNU Affero General Public License v3.0"
__version__ = "0.0.1"
__maintainer__ = "Christian Unterbergt"
__email__ = "chris@tetsuoanimation.com"
__links__ = ["https://www.tetsuoanimation.com", "https://tetsuoanimation.gumroad.com/l/mayaspeedslider"]
__status__ = "Release"

"""
    If this script helps you, feel free to say "Hi", check out our work or even hire us 
    ( You can also tip us a coffee if you liked the script )
    Oh and .. share your tools with the community - it helps everyone ;)
"""

import maya.cmds as cmds
from shiboken2 import wrapInstance
from PySide2 import QtCore, QtGui, QtWidgets
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin
import maya.OpenMayaUI as omui
import os, tempfile, sys

def maya_main_window():
    # Return the Maya main window as QMainWindow
    main_window = omui.MQtUtil.mainWindow()
    if sys.version_info.major >= 3:
        return wrapInstance(int(main_window), QtWidgets.QWidget)
    else:
        return wrapInstance(long(main_window), QtWidgets.QWidget) # type: ignore

class TempoSlider(MayaQWidgetDockableMixin, QtWidgets.QDialog):
    def __init__(self):
        super(TempoSlider, self).__init__(maya_main_window())
        self.setWindowTitle("Playback Tempo Tool")
        self.resize(400, 20)

        self.create_ui()
        self.set_defaults()
        self.create_connections()
        
    def create_ui(self):
        self.wgt_temposlider = QtWidgets.QSlider(orientation=QtCore.Qt.Horizontal)
        self.wgt_temposlider.setMinimum(0)
        self.wgt_temposlider.setMaximum(1000)
        self.wgt_temposlider.setValue(1000)
        # self.wgt_temposlider.setTickPosition(QtWidgets.QSlider.TicksBelow)
        # self.wgt_temposlider.setTickInterval(100)
        
        self.wgt_rate_spinbox = QtWidgets.QDoubleSpinBox()
        self.wgt_rate_spinbox.setMinimum(0)
        self.wgt_rate_spinbox.setMaximum(1)
        self.wgt_rate_spinbox.setValue(1)
        self.wgt_rate_spinbox.setSingleStep(0.01)

        self.wgt_btn_reset = QtWidgets.QPushButton("Reset")
        self.wgt_chkbx_showsubframes = QtWidgets.QCheckBox("Show Subframes")
        
        lyt_slider = QtWidgets.QHBoxLayout()
        lyt_slider.addWidget(self.wgt_temposlider)


        lyt_buttons = QtWidgets.QHBoxLayout()
        lyt_buttons.addWidget(QtWidgets.QLabel("Playback Rate:"))
        lyt_buttons.addWidget(self.wgt_rate_spinbox)

        lyt_buttons.addItem(QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding))
        lyt_buttons.addWidget(self.wgt_chkbx_showsubframes)
        lyt_buttons.addWidget(self.wgt_btn_reset)
        
        lyt_main = QtWidgets.QVBoxLayout(self)
        lyt_main.addLayout(lyt_slider)
        lyt_main.addLayout(lyt_buttons)
    
    def create_connections(self):
        self.wgt_temposlider.valueChanged.connect(self.slider_value_changed)
        self.wgt_rate_spinbox.valueChanged.connect(self.box_value_changed)
        self.wgt_btn_reset.clicked.connect(self.reset_rate)
        self.wgt_chkbx_showsubframes.clicked.connect(self.show_subframes_changed)

    def set_defaults(self):
        self.wgt_chkbx_showsubframes.setChecked(True)
        
    def slider_value_changed(self):
        self.wgt_temposlider.value()
        val = round(self.wgt_temposlider.value()/1000.0, 2)
        self.wgt_rate_spinbox.setValue(val)
        self.set_playrate(val)
        
    def box_value_changed(self):
        val = round(self.wgt_rate_spinbox.value(), 2)

        self.set_playrate(val)

    def reset_rate(self):
        target_rate = 1
        self.wgt_temposlider.setValue(target_rate*1000)
        self.wgt_rate_spinbox.setValue(target_rate)
        self.set_playrate(target_rate)

    def show_subframes_changed(self):
        self.set_playrate(round(self.wgt_rate_spinbox.value(), 2))
    
    def set_playrate(self, target_rate):
        if target_rate<1 and self.wgt_chkbx_showsubframes.isChecked():
            cmds.playbackOptions(maxPlaybackSpeed=target_rate)
            cmds.playbackOptions(playbackSpeed=0)
            cmds.playbackOptions(by=target_rate)
        else:
            cmds.playbackOptions(playbackSpeed=target_rate)
            cmds.playbackOptions(by=target_rate)

def start():
    global tempslider_window
    try:
        tempslider_window.close()
        tempslider_window.deleteLater()
    except:
        pass
    tempslider_window = TempoSlider()
    tempslider_window.show(dockable=True)

if __name__ == "__main__":
    start()
